--region SkeletonAnimationCache.lua
--Author : kevinz
--Date   : 2015/06/08
--Description ：骨骼动画对象缓存池



SKE_ANIMATION_TYPE_FREE = 1
SKE_ANIMATION_TYPE_ALREADY_ADDCHILD = 2

SkeletonAnimationCache = class("SkeletonAnimationCache",function()
    local ret = {
            --未使用的对象
            dicFreeList = {},
            --正在使用的对象
            dicUsedList = {},
        }
        return ret
    end)

SkeletonAnimationCache.__index = SkeletonAnimationCache
SkeletonAnimationCache.__instance = nil

function SkeletonAnimationCache:getInstance()
    local ret = nil
    if SkeletonAnimationCache.__instance == nil then
        ret = SkeletonAnimationCache:new()
        SkeletonAnimationCache.__instance = ret
    else 
        ret = SkeletonAnimationCache.__instance
    end
    return ret
end

function SkeletonAnimationCache:createWithFile(skeletonDataFile,atlasFile,scale)
    if not self.dicFreeList[skeletonDataFile] or #self.dicFreeList[skeletonDataFile] <= 0 then
        self.dicFreeList[skeletonDataFile] = {}
        for i=1,10 do
            local animation = sp.SkeletonAnimation:createWithFile(skeletonDataFile,atlasFile,scale)
            local dicAnimation = {}
            dicAnimation.status = SKE_ANIMATION_TYPE_FREE
            dicAnimation.animation = animation
            table.insert(self.dicFreeList[skeletonDataFile], dicAnimation)
        end
    end
    log.debug("SkeletonAnimationCache:createWithFile","skeletonDataFile(%s)dicFreeList-size(%d)", skeletonDataFile,#self.dicFreeList[skeletonDataFile])
end

function SkeletonAnimationCache:getSkeAnimation(skeletonDataFile)
    log.debug("SkeletonAnimationCache:getSkeAnimation","dicFreeList-size(%d)", #self.dicFreeList[skeletonDataFile])
    if #self.dicFreeList[skeletonDataFile] <= 0 then
        local sJson = clone(skeletonDataFile)
        local sAtlas = string.gsub(skeletonDataFile, ".json", ".atlas")
        self:createWithFile(sJson,sAtlas,1)
    end

    local dicAnimationInfo = self.dicFreeList[skeletonDataFile][1]
    --从空闲列表中删除 加入到正在使用的列表中
    table.remove(self.dicFreeList[skeletonDataFile],1)
    if not self.dicUsedList[skeletonDataFile] then
        self.dicUsedList[skeletonDataFile] = {}
    end
    log.debug("SkeletonAnimationCache:getSkeAnimation","skeletonDataFile(%s)dicUsedList-size(%d)", skeletonDataFile,#self.dicUsedList[skeletonDataFile])

    local dicAnimation = {}
    dicAnimation.status = SKE_ANIMATION_TYPE_ALREADY_ADDCHILD
    dicAnimation.animation = dicAnimationInfo.animation
    table.insert(self.dicUsedList[skeletonDataFile], dicAnimation)
    return dicAnimationInfo.status,dicAnimationInfo.animation
end

function SkeletonAnimationCache:removeAnimation(animation)
    log.debug("SkeletonAnimationCache:removeAnimation","start")
    for skeletonDataFile,arrAnimationList in pairs(self.dicUsedList) do
        for i,dicAnimationInfo in ipairs(arrAnimationList) do
            if dicAnimationInfo.animation == animation then
                --将当前的对象从正在使用的列表中移除 同时加入到空闲的队列中
                table.remove(arrAnimationList,i)
                dicAnimationInfo.status = SKE_ANIMATION_TYPE_ALREADY_ADDCHILD
                table.insert(self.dicFreeList[skeletonDataFile],dicAnimationInfo)
                log.debug("SkeletonAnimationCache:removeAnimation","exit skeletonDataFile(%s)dicFreeList-size(%d)dicUsedList-size(%d)", 
                    skeletonDataFile, #self.dicFreeList[skeletonDataFile], #self.dicUsedList[skeletonDataFile])
                return
            end
        end
    end
    log.debug("SkeletonAnimationCache:removeAnimation","end")
end